package org.wjy.post.dao.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;
import org.wjy.post.dao.GoodsDao;

import org.wjy.post.util.BaseDao;
import org.wjy.post.vo.Goods;
import org.wjy.post.vo.User;
/**     
 * 类名称：UserDaoImpl   
 * 类描述：用户控制dao实现数据库操作接口实现
 * 创建人：anan   
 * 创建时间：2012-12-21 下午11:07:43   
 * 修改人：anan  
 * 修改时间：2012-12-21 下午11:07:43   
 * 修改备注：   
 * @version        
 * */
@Repository("goodsDao")
public class GoodsDaoImpl extends BaseDao implements GoodsDao{

	@Override
	public List getgoodslist() {
		// TODO Auto-generated method stub
		Session session = getSession();
		Transaction tc = (Transaction) session.beginTransaction();
		Query query=session.createQuery("from Goods g");
		return query.list();
	}

	@Override
	public boolean addGoods(Goods good) {
		// TODO Auto-generated method stub
		Session session = getSession();
		Transaction tc = (Transaction) session.beginTransaction();
		session.save(good);
		try {
			tc.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		session.close();
		return true;
	}

	@Override
	public boolean delGoods(int id) {
		Session session = this.getSession();
		Transaction tc = (Transaction) session.beginTransaction();
		Goods u = new Goods(id);
		session.delete(u);
		try {
			tc.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		session.close();
		return true;
	}
	
	@Override
	public boolean updateGoods(Goods good) {
		Session session = this.getSession();
		Transaction tc = (Transaction) session.beginTransaction();
		session.update(good);
		try {
			tc.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		session.close();
		return true;
	}

	@Override
	public double getprice(String goodid) {
		// TODO Auto-generated method stub
		Session session = this.getSession();
		Transaction tc = (Transaction) session.beginTransaction();
		Query query=session.createQuery("select price from Goods where goodsnum=?");
		query.setParameter(0, goodid);
		List list=query.list();
		if(list.size()>0){
			return (double) list.get(0);
		}
		return 0;
	}
	
		

}
